package flown.window;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JFrame;

import flown.window.listener.MouseListener;
import flown.window.listener.WindowListener;
import flown.window.ui.Widget;
import flown.window.ui.组件;

/**
 * @author flown
 */


public class Dialog
{
	private final JDialog dialog;///<

	/**
	 * 初始化
	 * */
	public Dialog()
	{
		this.dialog = new JDialog();
		this.dialog.setLocationRelativeTo(null);							//设置窗口居中
		this.dialog.setLayout(new GridLayout());							//设置窗口布局
		this.dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);	//设置窗口默认CloseOperation
	}
	public Dialog(String title){this();this.dialog.setTitle(title);}
	public Dialog(String title,int width,int height){this(title);this.dialog.setSize(width,height);}



	/**
	 * 初始化
	 * @param window flown.window.Window
	 * */
	public Dialog(Window window)
	{
		this.dialog = new JDialog(window.GetFrame());
		this.dialog.setLocationRelativeTo(null);							//设置窗口居中
		this.dialog.setLayout(new GridLayout());							//设置窗口布局
		this.dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);	//设置窗口默认CloseOperation
	}

	/**
	 * 初始化
	 * @param window flown.window.Window
	 * @param title 窗口标题
	 */
	public Dialog(Window window,String title){this(window);dialog.setTitle(title);}

	/**
	 * 初始化
	 * @param window flown.window.Window
	 * @param title 窗口标题
	 * @param width 窗口宽度
	 * @param height 窗口高度
	 */
	public Dialog(Window window,String title,int width,int height)
	{
		this(window,title);
		this.dialog.setSize(width, height);
	}



	/**
	 * 初始化
	 * @param window flown.window.窗口
	 * */
	public Dialog(窗口 window)
	{
		this.dialog = new JDialog(window.获取窗口());
		this.dialog.setLocationRelativeTo(null);							//设置窗口居中
		this.dialog.setLayout(new GridLayout());							//设置窗口布局
		this.dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);	//设置窗口默认CloseOperation
	}

	/**
	 * 初始化
	 * @param window flown.window.窗口
	 * @param title 窗口标题
	 */
	public Dialog(窗口 window,String title){this(window);this.dialog.setTitle(title);}

	/**
	 * 初始化
	 * @param window flown.window.窗口
	 * @param title 窗口标题
	 * @param width 窗口宽度
	 * @param height 窗口高度
	 */
	public Dialog(窗口 window,String title,int width,int height)
	{
		this(window,title);
		this.dialog.setSize(width, height);
	}



	/**
	 * 设置窗口可视
	 * @param state state若为真，会让这个Dialog可视
	 * */
	public final void SetVisible(boolean state){this.dialog.setVisible(state);}

	/**
	 * 设置窗口布局
	 * @param manager 要设置的布局
	 * */
	public final void SetLayout(LayoutManager manager) {this.dialog.setLayout(manager);}

	/**
	 * 获取JDialog
	 * @return 返回JDialog
	 * */
	public final JDialog GetDialog(){return this.dialog;}



	public final int GetWidth(){return this.dialog.getWidth();}
	public final int GetHeight(){return this.dialog.getHeight();}


	/**
	 * 设置窗口图标
	 * @param path 图标文件路径
	 * */
	public final void SetIcon(String path) throws IOException
	{
		Image img = ImageIO.read(this.dialog.getClass().getResource(path));//读取路径图片
		this.dialog.setIconImage(img);//设置图片为窗口图标
	}

	/**
	 * 设置窗口图标
	 * @param input url
	 * */
	public final void SetIcon(URL input) throws IOException
	{
		Image img = ImageIO.read(input);//读取图片
		this.dialog.setIconImage(img);//设置图片为窗口图标
	}

	/**
	 * 设置窗口图标
	 * @param img 图标图像
	 * */
	public final void SetIcon(Image img){this.dialog.setIconImage(img);}


	/**
	 * 设置菜单栏
	 * @param menu_bar 要设置的菜单栏
	 * */
	public final void SetMenuBar(MenuBar menu_bar)
	{
		this.dialog.setJMenuBar(menu_bar.GetMenuBar());
	}

	/**
	 * 设置菜单栏
	 * @param menu_bar 要设置的菜单栏
	 * */
	public final void SetMenuBar(菜单栏 menu_bar)
	{
		this.dialog.setJMenuBar(menu_bar.获取菜单栏());
	}


	/**
	 * 添加鼠标监听器
	 * @param listener 要设置的监听器
	 * */
	public final void AddMouseListener(MouseListener listener)
	{
		this.dialog.addMouseListener(listener);
		this.dialog.addMouseMotionListener(listener);
		this.dialog.addMouseWheelListener(listener);
	}

	/**
	 * 添加key监听器
	 * @param listener 要设置的监听器
	 * */
	public final void AddKeyListener(KeyListener listener)
	{
		this.dialog.addKeyListener(listener);
	}

	/**
	 * 添加窗口监听器
	 * @param listener 要设置的监听器
	 * */
	public final void AddWindowListener(WindowListener listener)
	{
		this.AddMouseListener(listener);
		this.AddKeyListener(listener);
		this.dialog.addWindowListener(listener);
	}


	/**
	 * 给Dialog添加组件
	 * @param comp 要添加的组件
	 * */
	public final void Add(Component comp){this.dialog.getContentPane().add(comp);}

	/**
	 * 给Dialog添加组件
	 * @param widget 要添加的组件
	 * */
	public final void Add(Widget widget){this.dialog.getContentPane().add(widget.Get());}

	/**
	 * 给Dialog添加组件
	 * @param widget 要添加的组件
	 * */
	public final void Add(组件 widget){this.dialog.getContentPane().add(widget.获取());}

}











